// areaLeader/pages/areaIndex/areaIndex.js
var common = require("../../../common/common.js").common;

var colorParse = require("../../../common/color.js");

var {$author: $author} = require("../../../components/base/authorization.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        goBack: true,
        date_check: false,
        dateMonth: "历史",
        title: "区域团长管理中心",
        date_text: "今日",
        active: 1,
        plat_time: [ {
            text: "今日",
            date: "TdateFormate"
        }, {
            text: "昨日",
            date: "YdateFormate"
        }, {
            text: "本周",
            date: "WeekFormate"
        }, {
            text: "本月",
            date: "MonthFormate"
        } ]
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.requestStart();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        console.log(this.data);
        let that = this;
        return {
            title: that.data.leader_area_set.recommend_title,
            path: "/pages/group_info/group_info?leader_area_id=" + that.data.info.leader_area_id,
            imageUrl: that.data.leader_area_set.recommend_img
        };
    },
    requestStart: function() {
        let that = this;
        that.getBase();
        that.getData();
        that.queryData();
        //交易数据
                that.getLeaderDataAll();
        //数据总览
        },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            data: {},
            success: res => {
                var copyText = [];
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                var rgba = colorParse.hexToRgb(res.extra.color_main, .8);
                that.setData({
                    color_main: res.extra.color_main,
                    color_main2: rgba,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    copyImg: res.extra.copyright_img,
                    copyText: copyText,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    vip_card_on: res.extra.vip_card_on,
                    new_member_audit: res.extra.new_member_audit,
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2,
                    leader_area_set: res.extra.leader_area_set
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
                if (that.data.leader_area_set.is_recommend != 1) {
                    wx.hideShareMenu();
                }
            }
        });
    },
    getData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                that.setData({
                    info: res.extra
                });
            }
        });
    },
    dateCheckChange() {
        this.setData({
            active: "5",
            date_check: !this.data.date_check
        });
    },
    bindDateChange(e) {
        console.log("picker发送选择改变，携带值为", e.detail.value);
        this.setData({
            active: e.detail.value,
            dateMonth: e.detail.value,
            date_check: false
        });
        this.queryData();
    },
    queryData(e) {
        let that = this;
        var active = that.data.active;
        var date_text = that.data.date_text;
        if (e) {
            active = e.currentTarget.dataset.index;
        }
        if (active == 1) {
            date_text = "今日";
        } else if (active == 2) {
            date_text = "昨日";
        } else if (active == 3) {
            date_text = "本周";
        } else if (active == 4) {
            date_text = "本月";
        } else {
            date_text = active;
        }
        that.setData({
            date_text: date_text,
            active: active
        });
        common.request({
            endpoint: "leader-area/leader-area-today",
            method: "post",
            data: {
                date: that.data.active
            },
            success: res => {
                that.setData({
                    leaderAreaData: res.extra
                });
            }
        });
    },
    getLeaderDataAll() {
        let that = this;
        common.request({
            endpoint: "leader-area/leader-area-data-all",
            method: "post",
            data: {},
            success: res => {
                console.log(res);
                that.setData({
                    leaderDataAll: res.extra
                });
            }
        });
    },
    goToLeaderFunction(e) {
        common.getUserInfoNew({
            url: e.currentTarget.dataset.url
        });
    }
});